<?php

$f3=require('../lib/base.php');

$f3->set('DEBUG',0);
$f3->set('CACHE',true);
$f3->set('UI','../views/');
$f3->config('../setup.cfg');

$f3->route('GET /',
           function($f3) {
               $url="http://wapiknow.baidu.com/m/recommend";
               $home = '';
               $req = \Web::instance()->request($url);
               $f3->set("site_keywords", '<meta name="keywords" content="150019,问题网,为什么,找答案，问答系统 提问 回答 知识搜索">');
               $f3->set('site_desc', '<meta name="description" content="www.150019.com - 问题网,找到所有问题的答案.">');
               if (isset($req['body'])) {
                   $html = $req['body'];
                   $html = preg_replace('/[\n\r\t]/i','',$html);
                   preg_match_all('/<a href="http:\/\/zhidao\.baidu\.com[\s\S]*?<\/a>/i',$html,$list);

                   $htmllist = '<ul id="rc-list" class="rc-list">';

                   foreach($list[0] as $a) {
                      $htmllist .= '<li><i class="icon-question-sign"></i>'.$a.'</li>';
                   }

                   $htmllist .= "</ul>";

                   $data = $htmllist;

                   $empty = array(
                       '/<span class="answerer">[\s\S^<]*<\/span>/iU',
                       '/<script[\s\S^>]*>*<\/script>/iU'
                       );
                   $data = preg_replace($empty,'',$data); 
                   $data = str_replace("zhidao.baidu.com", 'www.150019.com', $data);
                   $data = str_replace("jingyan.baidu.com", 'www.150019.com', $data);
                   $data = str_replace("/question/", '/question-', $data);

                   $home = $data;
               }

               $f3->set('home', $home);
               $f3->set("css", "home.css");
               echo View::instance()->render('home.html');
           },0//24*3600
    );

$f3->route('GET /category-*', 
           function ($f3) {
               $url = $f3->get("PARAMS");
               $this_link = $url[0];
               $ps = explode("-", $url[0]);
               $cat = $ps[1];
               $subcat = $ps[2];
               $page = $ps[3];
               $host = "http://wapiknow.baidu.com/browse/$cat?lm=$subcat&pn=$page";

               $f3->set("id",$cat);

               $breadcrumbs = "";
               $category = "";
               $list = "";
      	       $pages = "";

               $req = \Web::instance()->request($host);
               $f3->set("site_keywords", '<meta name="keywords" content="150019,问题网,为什么,找答案，问答系统 提问 回答 知识搜索">');

               if (isset($req['body'])) {
                   $raw = $req['body'];
                   $raw = preg_replace('/[\n\r\t]/i','',$raw);

                   preg_match('/<title>.*?<\/title>/i', $raw, $title);

                   preg_match('/<meta name="keywords" content="[\s\S]*?>/i', $raw, $keywords);
                   if(isset($title[0])) {
                       $title = str_replace(array("知道"),array("问题网"),$title[0]);
                       $title = str_replace(array("百度"),array(""),$title);

                       $f3->set("site_name", $title);
                   }

                   if ($keywords != null) {
                       $keywords = str_replace(array("知道"),array("问题网"),$keywords);
                       $f3->set("site_keywords", $keywords[0]);
                   }


                   $f3->set("site_desc",'<meta name="description" content="www.150019.com - 问题网,找到所有问题的答案."/>');

                   preg_match('/<body>[\s\S]*?<\/body>/i',$raw,$tmp);
                   $raw = $tmp[0];

                   preg_match('/<div class="ft"[\s\S]*?<\/div>/i',$raw,$tmp);
                   if (isset($tmp[0])) {
                       $breadcrumbs = $tmp[0];
                       $breadcrumbs = str_replace('href="/browse/"', 'href="/"',$breadcrumbs);
                       $breadcrumbs = preg_replace('/wapiknow\.baidu\.com/i','www.150019.com',$breadcrumbs);
                       $breadcrumbs = str_replace('知道','问题网',$breadcrumbs);

                       $breadcrumbs = preg_replace('/\/browse\/([0-9]*)/i', '/category-$1-4-0',$breadcrumbs);

                       $breadcrumbs = preg_replace('/\?[\s\S^"]*?"/i','"',$breadcrumbs);
                       $links = array();
                       preg_match_all('/<a[\s]*href="([\s\S]*?)"\s*>([\s\S]*?)<\/a>/i', $breadcrumbs,$bbs);
                       for($i=0; $i< count($bbs[0]); $i++) {
                           $links[$i]["href"] = $bbs[1][$i];
                           $links[$i]["text"] = $bbs[2][$i];
                       }
                       preg_match_all('/.*>([\s\S]*?)<\/div>/i', $breadcrumbs, $lastitem);
                       $idx = count($links);
                       $links[$idx]["href"] = $this_link;
                       $links[$idx]["text"] = $lastitem[1][0];

                       $breadcrumbs = Web\Url::instance()->generateBreadcrumbs($links);

                       preg_match_all('/<a href="\/question\/[0-9]*[\s\S]*?<\/a>/i',$raw,$list2);

                       $list = '<ul id="questions-list">';
                       foreach($list2[0] as $a) {
                          $a = preg_replace('/\?[\s\S]*?"/i','"',$a);
                           $list .= '<li class="q-item"><i class="icon-question-sign"></i>'.$a.'</li>';
                       }

                       $list .= "</ul>";
                       $list = str_replace('/question/','/question-',$list);

//                       $list = preg_replace('/\/question-([0-9]*\.html)([\s\S]*)?"/i','/question-$1',$list);

                       preg_match('/<div class="n-re">[\s\S]*?<\/div>/i',$raw, $tmp);
                       if (isset($tmp[0])) {
                         $category = $tmp[0];
                         $category = preg_replace('/\/browse\/([0-9]*)/i','/category-$1-4-0',$category);

                         $category = preg_replace('/\?[\s\S^"]*?"/i','"',$category);
                         $category = str_replace('class="red"','',$category);
                         preg_match_all('/<a\s*href="[\s\S]*?<\/a>/i',$category,$as);

                         $category = '<ul class="alert nav nav-pills">';
                         foreach ($as[0] as $a) {
                           $category .= "<li>" .$a.' </li>';
                         }
                         $category .= '</ul>';

                       }
                       preg_match('/<table[\s\S]*?<\/table>/i',$raw,$tmp);
                       if (isset($tmp[0])) {
                       preg_match('/<td align="left">([\s\S]*?)<\/td>/i',$tmp[0],$left);
                       preg_match('/<span class="n-co5">[\s\S]*?<\/span>/i',$tmp[0],$right);
                       $pages = '<div class="pagination"><ul>';

                       $left = str_replace(array('<a','</a>','<span class="gray">','</span>'),array('<li><a','</a></li>','<li class="active"><a href="javascript:void(0);">','</a></li>'),$left[1]);
                       $reg = '/\/browse\/([0-9]*).*?lm=([0-9]*).*?pn=([0-9]*).*?"/i';
                       $left = preg_replace($reg,'/category-$1-$2-$3"',$left);
                       preg_match_all('/<a[\s\S]*?<\/a>/i',$left,$aas);
                       $pages .= '<li>' .$aas[0][1].'</li>'.'<li>'.$aas[0][0].'</li>';

                       $pages .= str_replace('</li>','</a></li>',str_replace(array('span','n-co5">'),array('li','active"><a href="javascript:void(0);">'),$right[0]));

                       $pages .= "</ul></div>";

                      }

                   }
               }

               $f3->set("breadcrumbs",$breadcrumbs);
               $f3->set("category",$category);
               $f3->set("list",$list);
               $f3->set("breadcrumbs",$breadcrumbs);
               $f3->set("pages",$pages);
               $f3->set("css", "category.css");
               echo View::instance()->render('category.html');

           },0//5*3600
    );

$f3->route('GET /question-*',
           function($f3) {
               $qtitle = '';
               $qcontent = '';
               $accepted = '暂无';
               $others = '';
               $related = '';

               $url = $f3->get("PARAMS");
               $url = preg_replace('/question-([0-9]*)-[0-9]*\.html/i','question-$1.html',$url);
               $this_link = $url;

               $f3->set("site_keywords", '<meta name="keywords" content="150019,问题网,为什么,找答案，问答系统 提问 回答 知识搜索">');
               $f3->set('site_desc', '<meta name="description" content="www.150019.com - 问题网,找到所有问题的答案.">');

               $url = preg_replace('/\/question-([0-9]+)\.html(.*)/i',"http://wapiknow.baidu.com/question/$1.html$2",$url);
               $req = \Web::instance()->request($url[0]);
               $f3->set("debug_headers",$req['headers']);

               if (isset($req['body'])) {
                   $raw = $req['body'];
                   $raw = preg_replace('/[\n\r\t]/i','',$raw);
                   preg_match('/<title>.*?<\/title>/i', $raw, $title);
                   preg_match('/<meta name="keywords" content="[\s\S]*?>/i', $raw, $keywords);
                   preg_match('/<meta name="description" content="[\s\S]*?\/>/i', $raw, $desc);
                   if(isset($title[0])) {
                       $title = str_replace(array("百度"),array(""),$title[0]);
                       $title = str_replace(array("知道"),array("问题网"),$title);
                       $f3->set("site_name", $title);
                   }

                   if ($keywords != null) {
                       $keywords = str_replace(array("百度"),array(""),$keywords);
                       $keywords = str_replace(array("知道"),array("问题网"),$keywords);
                       $f3->set("site_keywords", $keywords[0]);
                   }

                   if ($desc != null) {
                       $desc = str_replace(array("百度"),array(""),$desc);
                       $desc = str_replace(array("知道"),array("问题网"),$desc);
                       $f3->set("site_desc", $desc[0]);

                   }

                   preg_match('/<p class="b">([\s\S]*?)<\/p>/i',$raw,$tmp);
		               if (isset($tmp[1]))
                     $qtitle = $tmp[1];

                   preg_match('/<div class="ra ft">([\s\S]*?)<div class="ft">/i',$raw,$tmp);

                   if (isset($tmp[1])) {

                     $related = $tmp[1];
                     preg_match_all('/<a href.*?<\/a>/i',$related, $rls);

                     $related = "<ul>";
                     foreach($rls[0] as $a) {

                       $a = preg_replace('/\?[\s\S]*?"/i','"',$a);
                       $a =str_replace('/question/','/question-',$a);
                       $related .= '<li>» '.$a.'<li>';
                     }

                     $related .= "</ul>";
                   }


                   preg_match('/<pre class="line mt-10 q-content" accuse="qContent">([\s\S]*?)<\/pre>/i',$raw,$qc);
                   if (isset($qc[1]))
                      $qcontent = $qc[1];



                   preg_match('/<div class="bt bg2 ft n-co1">[\s\S]*?<div>/i',$raw,$tmp);

                   if (isset($tmp[0])) {
                     preg_match('/<p class="ft p1">[\s\S]*<\/p>/i',$tmp[0],$ttt);

                     if (isset($ttt)) {
                        $accepted = $ttt[0];
                        $accepted = preg_replace('/<a href=".*?<\/a>/i','',$accepted);
                     }

                   }
                   preg_match_all('/<p class="ft bb">[\s\S]*?<\/p>/i',$raw ,$tmp);
                   if (isset($tmp) && count($tmp[0]) >0) {
                     $others .= '<ul id="others">';
                     foreach ($tmp[0] as $ll) {
                       $ll = preg_replace('/<a href=".*?<\/a>/i','',$ll);
                       $others .= "<li>" .$ll .'</li>';
                     }

                     $others .= "</ul>";
                   }

               }

               $f3->set('qtitle',$qtitle);
               $f3->set('qcontent',$qcontent);
               $f3->set('accepted', $accepted);
               $f3->set('related',$related);
               $f3->set('others',$others);
               $f3->set('css', 'question.css');
               echo View::instance()->render('question.html');
           },0//5*3600
    );

$f3->route('GET /yxlist-*', 
  function ($f3) {
   $urls = explode("-",$f3->get('PARAMS')[0]);
   $cat = $urls[1];
   $this_link = $f3->get('PARAMS')[0];
   if ($cat == "all") 
     $cat = "_all";

   $cat = str_replace("_all","/",$cat);


   $host = "http://www.120ask.com/list/".$cat."over/".$urls[2].'/';

   $breadcrumbs = '';
   $category = '';
   $list = '';
   $pages = '';
   $req = \Web::instance()->request($host);
   if (isset($req['body'])) {
     $raw = $req['body'];
     preg_match_all('/<p class="h-pp1">[\s\S]*?<\/p>/i',$raw, $tmp);
     if (isset($tmp[0])) {

       $list .= '<ul id="question-list">';
       foreach ($tmp[0] as $a2) {
         preg_match_all('/(<a href="\/list\/[a-z]*\/over\/">[\s\S]*?<\/a>)(<a class="q-quename"[\s\S]*?<\/a>)/i',$a2,$as);
         $ll = str_replace('<a','<a class="label badge-important"',$as[1][0]);
         $ll = str_replace('/list/','/yxlist-',$ll);
         $ll = str_replace('/over/','_all-1',$ll);
         $list .= '<li class="q-item"><i class="icon-question-sign"></i>'.$as[2][0].$ll.'</li>';
       }

       $list .= '</ul>';
     }

     preg_match('/<ul class="clears h-ul1">[\s\S]*?<\/ul>/i',$raw,$tmp);
     if (isset($tmp[0])) {
       $category = str_replace('clears', 'alert nav nav-pills', $tmp[0]);

       $category = str_replace('/list/','/yxlist-',$category);
       $category = str_replace('/over/','_all-1',$category);
     }

     preg_match('/<p class="fl">[\s\S]*?<\/p>/i',$raw,$tmp);
     if (isset($tmp[0])) {
       $breadcrumbs = $tmp[0];

       $breadcrumbs = str_replace('/list/','/yxlist-',$breadcrumbs);
       $breadcrumbs = str_replace('/over/','_all-1',$breadcrumbs);
       $breadcrumbs = str_replace('首页','健康首页',$breadcrumbs);
       $breadcrumbs = str_replace('"/yxlist-"','"/yxlist-all-1"',$breadcrumbs);

       $links = array();
       preg_match_all('/<a[\s]*href="([\s\S]*?)"\s*>([\s\S]*?)<\/a>/i', $breadcrumbs,$bbs);
         for($i=0; $i< count($bbs[0]); $i++) {
           $links[$i]["href"] = $bbs[1][$i];
           $links[$i]["text"] = $bbs[2][$i];
         }
       }

                       preg_match('/.*<\/a>([\s\S]*?)<\/p>$/i', $breadcrumbs, $lastitem);
                       $idx = count($links);
                       $links[$idx]["href"] = $this_link;
                       $links[$idx]["text"] = $lastitem[1];
                       $breadcrumbs = Web\Url::instance()->generateBreadcrumbs($links);

   }


   preg_match('/<div class="clears h-page">[\s\S]*?<\/div>/i',$raw,$tmp);
   if (isset($tmp[0])) {
     $pages .= '<div class="pagination">';
     $pages .= str_replace(array('div','span','h-aa2','[',']'), array('ul','li','active','',''),$tmp[0]); 
     $pages .= '</div>' ;
     $pages = preg_replace('/\/list\/([a-z]*)\/[a-z]*\/([0-9]*)\//i','/yxlist-$1_all-$2',$pages);
     $pages = preg_replace('/[\r\n\t]/i','',$pages);
     $pages = str_replace("<li><a class='active'",'<li class="active"><a ',$pages);

   }

   $f3->set("breadcrumbs", $breadcrumbs);
   $f3->set("category", $category);
   $f3->set("list", $list);
   $f3->set("pages", $pages);
   $f3->set('css','yxlist.css');
   echo View::instance()->render('yxlist.html');
  },0//5*3600
);


$f3->route('GET /question/*',
  function($f3) {
    $qtitle = '';
               $qcontent = '';
               $accepted = '暂无';
               $others = '';
               $related = '';

               $url = $f3->get("PARAMS")[0];
               $this_link = $url;

              $furl = "http://www.120ask.com".$url;
               $req = \Web::instance()->request($furl);

               if (isset($req['body'])) {
                   $raw = $req['body'];
                   $raw = preg_replace('/[\n\r\t]/i','',$raw);

                   preg_match('/<title>[\s\S]*?<\/title>/i', $raw, $title);
                   preg_match('/<meta name="keywords" content="[\s\S]*?>/i', $raw, $keywords);
                   preg_match('/<meta name="description" content="[\s\S]*?\/>/i', $raw, $desc);
                   if(isset($title[0])) {
                       $f3->set("site_name", $title[0]);
                   }

                   if ($keywords != null) {
                       $f3->set("site_keywords", $keywords[0]);
                   }

                   if ($desc != null) {
                       $f3->set("site_desc", $desc[0]);

                   }

                   preg_match('/<h1 id="d_askH1">([\s\S]*?)<\/h1>/i',$raw,$tmp);
                   if (isset($tmp[1]))
                     $qtitle = $tmp[1];


                   preg_match('/<ul class="d_lblue clears"[\s\S]*?<\/ul>/i',$raw,$tmp);
                   if (isset($tmp[0])) {
                     $related = $tmp[0];

                     $related = str_replace('www.120ask.com','www.150019.com',$related);
                     $related = preg_replace('/onclick="[\s\S]*?"/i','',$related);
                     $related = preg_replace('/<script.*?<\/script>/i','',$related);
                   }


                   preg_match('/<div class="d_des">([\s\S]*?)<\/div>/i',$raw,$qc);
                   if (isset($qc[1]))
                      $qcontent = $qc[1];

                   preg_match_all('/(<div>[\s\S]*)?<div class="d_cn">/i',$raw,$tmp);
                   if (isset($tmp[1][0])) {

                     $accepted = $tmp[1][0];
                     $accepted = preg_replace('/<p class="d_lr d_txt999 d_sma">[\s\S]*?<\/p>/i','',$accepted);
                   }

                   preg_match_all('/(<div>[\s\S]*)?<div class="d_tobar p_commentbtn clears">/i',$raw,$tmp);

                   if (isset($tmp) && count($tmp[0]) >0) {
//                     $others  = $tmp[1][0];
                   }

               }

               $f3->set('qtitle',$qtitle);
               $f3->set('qcontent',$qcontent);
               $f3->set('accepted', $accepted);
               $f3->set('related',$related);
               $f3->set('others',$others);
               $f3->set('css', 'question.css');
               echo View::instance()->render('yx-question.html'); 


  },0//24*3600
);
$f3->route('GET /yx-*',
   function($f3) {
     $url = $f3->get('PARAMS')[0];
     $url = preg_replace('/\/yx-([0-9]*)\.html.*/','/question/$1.htm',$url);

     $f3->reroute($url,true);
   }
);

$f3->route('GET /questions/q*',
  function($f3) {
    $url = $f3->get('PARAMS')[0];
    $url = str_replace('s/q','-',$url);
    $f3->reroute($url,true);
  }
); 


/*$f3->route('GET /*',
   function($f3) {
	echo "<h1>the Server doing  maintenace. please check later,sorry</h1>";

});
*/
$f3->run();
